package com.xj.edu.hotel.controller;
import com.xj.edu.hotel.domain.User;
import com.xj.edu.hotel.service.MailService;
import com.xj.edu.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Random;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MailService mailService;
    /**
     * 用户信息查询
     */
    @RequestMapping("findUser")
    public String findAllUser(Model model){
        List<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "user-list";
    }
    /**
     * 用户信息删除
     */
    @RequestMapping("delUser")
    public String delUser(String id){
        int success = userService.delectById(id);
        return "redirect:/user/findAll";
    }
    /**
     * 用户注册
    /**
     *
     获取验证码
     */
    @RequestMapping("/getCheckCode")
    @ResponseBody
    public String getCheckCode(String email){
        String checkCode = String.valueOf(new Random().nextInt(899999)+100000);
        String message = "您的验证码:"+checkCode;
        try {
            System.out.println("-------------"+checkCode);
            mailService.sendSimpleMail(email,"注册验证码",message);
        }catch(Exception e){
            return "";
        }
        return checkCode;
    }

    @RequestMapping(value = "/userRegist",method = RequestMethod.POST )
    @ResponseBody
    public String regist(User user){
        System.out.println(user.getUserName());
           int success = userService.insert(user);
           if(success > 0 ){
               return "ok";
           }else {
               return "error";
           }
    }



}







